//
//  WXLogInViewController.m
//  Big Show
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "WXLogInViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "UIImageView+WebCache.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 667
@interface WXLogInViewController ()

@property (nonatomic, retain) UIImageView *img;
@property (nonatomic, retain) UIImageView *img1;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIButton *button2;

@end

@implementation WXLogInViewController

- (void)dealloc
{
    [_label release];
    [_img1 release];
    [_button2 release];
    [_img release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 背景图片
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createImageView];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _button2.frame = CGRectMake(WIDTH*80, HEIGHT*360,  self.view.frame.size.width- WIDTH*160, HEIGHT*40);
    [_button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _button2.backgroundColor = [UIColor colorWithRed:0.119 green:0.750 blue:0.727 alpha:1.000];
    _button2.titleLabel.font = [UIFont systemFontOfSize:17];
    [_button2.layer setCornerRadius:10];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"stand"]) {
        [_button2 setTitle:@"退出登录" forState:UIControlStateNormal];
    }else{
        [_button2 setTitle:@"微博登录" forState:UIControlStateNormal];
    }
    [_button2 addTarget:self action:@selector(denglu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(WIDTH*80, HEIGHT*420, self.view.frame.size.width- WIDTH*160, HEIGHT*40);
    [button3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button3.backgroundColor = [UIColor colorWithRed:0.119 green:0.750 blue:0.727 alpha:1.000];
    button3.titleLabel.font = [UIFont systemFontOfSize:17];
    [button3.layer setCornerRadius:10];
    [button3 setTitle:@"返回" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];

    

    self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH* 120, HEIGHT * 80, self.view.frame.size.width-WIDTH*240, HEIGHT *135)];
    [_img1.layer setCornerRadius:50];
    _img1.layer.masksToBounds = YES;// 图片超越边界的地方隐藏
    _img1.backgroundColor = [UIColor lightGrayColor];
    
    
    [_img1 sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"img"] ] placeholderImage:[UIImage imageNamed:@"占位图.jpg"]];
    [self.view addSubview:_img1];
    [_img1 release];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.img1.frame.origin.x, self.img1.frame.origin.y +self.img1.frame.size.height+HEIGHT*5, self.img1.frame.size.width, HEIGHT*35)];
    _label.backgroundColor = [UIColor whiteColor];
    _label.alpha = 0.5;
    _label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    [_label.layer setCornerRadius:10];
    
    [self.view addSubview:_label];
    [_label release];
    
    
    
    
    
    
    
    
    
   }

- (void)createImageView
{
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _img.image = [UIImage imageNamed:@"u=2916742164,3184761356&fm=21&gp=0.jpg"];
    [self.view addSubview:_img];
    [_img release];
}



- (void)denglu:(UIButton *)button
{
    
    
    if ([button.currentTitle isEqualToString:@"微博登录"]) {
        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo //平台类型
                          authOptions:nil //授权选项
                               result:^(BOOL result, id userInfo,  id error) { //返回回调
                                   if (result)
                                   {
                                       
                                       //                                       self.uid = [userInfo uid];
                                       //                                       self.nickname = [userInfo nickname];
                                       //                                       self.profileImage = [userInfo profileImage];
                                       
                                       NSLog(@"uid = %@",[userInfo uid]);
                                       NSLog(@"name = %@",[userInfo nickname]);
                                       NSLog(@"icon = %@",[userInfo profileImage]);
                                       
                                       [_button2 setTitle:@"退出登录" forState:UIControlStateNormal];
                                       [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"stand"];

                                       [[NSUserDefaults standardUserDefaults] setObject:[userInfo nickname] forKey:@"name"];
                                       [[NSUserDefaults standardUserDefaults] setObject:[userInfo profileImage] forKey:@"img"];
                                       [_img1 sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"img"]]];
                                       _label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];

                                   }
                                   
                               }];
       

    
    
    } else {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注销成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [a show];
        [a release];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"stand"];
        [[NSUserDefaults standardUserDefaults] setObject:@"未登录" forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"img"];
        [_button2 setTitle:@"微博登录" forState:UIControlStateNormal];
        [_img1 sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"img"]]];
        _label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    }
    
    
}



-(void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
